<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @author sonnk
 * @copyright 2011
 */
class Login extends CI_Controller {
	function __construct(){
		parent::__construct();			
	    $this->load->model();
		$this->pub->write('title',lang('title_task'));
	}
    function  _remap($method, $params = array()){
        if(method_exists($this, $method)){
            return call_user_func_array(array($this, $method), $params);
        }
        $this->_view();
    }
	function _view(){      
		$data = new stdClass();
		$this->pub->set_tpl('login');
		$logins = $this->pub->GetSession('logins');
		if(!empty($logins['username'])){
			redirect(base_url().'ketluangiaoban.html');	
		}
        $content = $this->load->view('view',$data,true);
        $this->pub->write('content',$content,true);
        $this->pub->render();
	}
	function captcha(){
        $captcha = $this->pub->Create_capcha('captcha_val');   
    }
	function checklogin(){
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$txtcaptcha = $this->input->post('txtcaptcha');
		$captcha = md5($txtcaptcha);
		$captcha2 = $this->pub->GetSession('captcha_val');
		if($captcha == $captcha2){
			$password = md5($username.(md5($password)).md5('SonNguyen@#$%&'));
			$query = $this->model->checklogin($username,$password);
			if(empty($query->username)){
				echo "Tài khoản, mật khẩu hoạc mã xác nhận không đúng.";
			}
			else{
				$arr = array();
				$arr['code'] = $query->code;
				$arr['username'] = $query->username;
				$arr['fullname'] = $query->fullname;
				$arr['groups'] = $query->groups;
				$arr['sex'] = $query->sex;
				$arr['depatment'] = $query->depatment;
				$arr['groupname'] = $query->groupname;
				$arr['types'] = $query->types;
				$this->pub->SetSession('logins',$arr);   
				echo 1;
			}
		}
		else{
			echo "Tài khoản, mật khẩu hoạc mã xác nhận không đúng.";
		}
	}
	function logouts(){
		$this->pub->DeleteSession('logins');   
		redirect(base_url().'login.html');		
	}
	function tt(){
        $this->load->model();
        $this->model->Gets();
    }
}